﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */


#region Using
using System;
using System.Collections.Generic;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using Rio.Framework.Historical;
using System.Data;
using NetBay.Core.BusinessEntities.Versioning;
using Rio.Doc.DataAccess;
using Rio.Framework.Extensions;
using Rio.Framework.Business;
using NetBay.Core.Tools;
using NetBay.Core.BusinessEntities.Historic;


#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    public class HistoricInfoTranslator
    {

        /// <summary>
        /// Historical dates list of consuls
        /// </summary>
        /// <param name="dbDataReader"></param>
        /// <returns></returns>
        public static List<HistoricInformation> TranslateHistoric(IDataReader dbDataReader)
        {
            var l_oHistoricInfoConsult = new List<HistoricInformation>();

            try
            {
                using (dbDataReader)
                {

                    while (dbDataReader.Read())
                    {
                        var historicInfoResultAdd = new HistoricInformation
                                                        {
                                                            Identifier = Convert.ToInt32(dbDataReader["HI_ID"].ToString()),
                                                            ActionDate = ConvertField.ToDateTime(dbDataReader["DATEACTION"].ToString()),
                                                            ActionTime = ConvertField.ToTimeSpan(DateTime.Parse(dbDataReader["HEUREACTION"].ToString())),
                                                            OrganizationName = dbDataReader["RE_NOM"].ToString(),
                                                            FoldertypeName = dbDataReader["RP_TD_LIBELLE"].ToString(),
                                                            DocumentsCountInFolder = Convert.ToInt32(dbDataReader["HII_NBDOC"].ToString()),
                                                            FoldersCountResultInSearch = Convert.ToInt32(dbDataReader["HII_NBRESULT"].ToString()),
                                                            IndexedDocumentsCount = Convert.ToInt32(dbDataReader["HII_NBINDEXED"].ToString()),
                                                            FolderTypeId = Convert.ToInt32(dbDataReader["RP_TD_ID"].ToString()),
                                                            OrganizationId = Convert.ToInt32(dbDataReader["RE_ID"].ToString()),
                                                            FolderId = Convert.ToInt32(dbDataReader["HI_IDDOC"].ToString()),
                                                            ItemName = dbDataReader["HII_INDEXNAME"].ToString(),
                                                            FolderIdDoc = dbDataReader["HII_FOLDERIDDOC"].ToString()
                                                        };


                        //Deserialize Indexes
                        List<HistoricIndex> tempList = SerializationHelper.Deserialize<List<HistoricIndex>>(dbDataReader["HiI_Index"].ToString());
                        historicInfoResultAdd.Indexes = tempList;

                        // Add to list
                        l_oHistoricInfoConsult.Add(historicInfoResultAdd);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oHistoricInfoConsult;
        }




    }
}
